package src;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;


public class DojoTimer {
	private static final int TIME = 7;
	private static JFrame window;
	private static int seconds;
	private static int minutes = TIME;
	private static JButton startButton;
	private static JLabel timeLabel; 
	private static boolean running = false;
	private static List<String> participants;
	
	public static void main(String[] args) {
		window = new JFrame("Dojo Timer");
		
		setAttributes();
		addComponents();
		window.pack();
		window.setLocationRelativeTo(null);
	}

	private static void setAttributes() {
		
		window.setVisible(true);
		window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	private static void addComponents() {
		window.add(getMainPanel());
		window.setMenuBar(getMenuBar());
	}

	private static JPanel getMainPanel() {
		JPanel panel = new JPanel();
		
		panel.setLayout(new BorderLayout());
		panel.add(getTimeLabel(), BorderLayout.CENTER);
		panel.add(getStartButton(), BorderLayout.SOUTH);
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		return panel;
	}

	private static MenuBar getMenuBar() {
		MenuBar menu = new MenuBar();
		
		menu.add(getMenu());
		return menu;
	}

	private static Menu getMenu() {
		Menu menu = new Menu("Options");
		
		menu.add(getAddPPlItem());
		menu.add(getExitItem());
		return menu;
	}


	private static MenuItem getExitItem() {
		MenuItem item = new MenuItem("Exit");
		
		item.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		return item;
	}

	private static MenuItem getAddPPlItem() {
		MenuItem item = new MenuItem("Add Participants");
		
		item.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				participants = selectParticipantsDialog();
			}

		});
		return item;
	}

	private static List<String> selectParticipantsDialog() {
		return new ParticipantsDialog(participants, getWindow()).selectParticipants();
	}
	
	private static JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton("Start");
			startButton.addActionListener(getButtonListener());
			startButton.setMaximumSize(new Dimension(50, 20));
		}
			return startButton;
	}

	private static ActionListener getButtonListener() {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!running) {
					startCountDown();
					running = true;
					startButton.setText("Stop");
				} else {
					stopCountDown();
					running = false;
					resetTimer();
					startButton.setText("Start");
				}
			}
		};
	}

	private static void startCountDown() {
		(new Thread() {
			public void run() {
				boolean warmUp = false;
				int warmUpTime = 10;
				
				setName("Timer");
				while (running) {
					
					if (reachedZero()) {
						new JDialog(window, "ACABOU!");
						timeLabel.setForeground(Color.RED);
						warmUp = true;
						setWarmUpTime();
					}
					
					if (warmUp) {
						if (warmUpTime > 0) {
							--warmUpTime;
						} else {
							warmUp = false;
							timeLabel.setForeground(Color.BLACK);
							resetTimer();
						}
					}
					
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (running) {
						--seconds;
						updateGui();
					}
				}
			}

		}).start();
	}
	
	protected static void setWarmUpTime() {
		minutes = 0;
		seconds = 10;
	}

	private static void resetTimer() {
		minutes = TIME;
		seconds = 0;
		updateGui();
	}

	private static void stopCountDown() {
		
	}

	protected static boolean reachedZero() {
		return minutes == 0 && seconds == 0;
	}

	private static void updateGui() {
		if (seconds == -1) {
			seconds = 59;
			--minutes;
		}
		timeLabel.setText(getNumberString(minutes) + ":" + getNumberString(seconds));
	}
	
	private static String getNumberString(int value) {
		if (value < 10) {
			return "0" + value; 
		}
		return String.valueOf(value);
	}

	private static JLabel getTimeLabel() {
		if (timeLabel == null) {
			timeLabel = new JLabel(getNumberString(minutes) + ":" + getNumberString(seconds));
			timeLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 64));
			timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return timeLabel;
	}
	
	public static JFrame getWindow() {
		return window;
	}
	
}
